<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/26
 * Time: 13:59
 */
namespace app\admin\controller;
use think\Controller;
use think\captcha\Captcha;
class Login extends Controller{


    public function _initialize(){
        if(session('admin_id')>0){
            $this->redirect(U('index/index'));
        }
    }

    public function index(){
      return  redirect(url('login'));//tp5的跳转方式
    }

    public function login(){

        if($_POST){
            $code=I("post.code",'','trim');
            $name=I("post.name",'','trim');
            $pwd=I("post.pwd",'');
            $g=new Captcha();
            if(!$g->check($code,'admin_login')){
                die(json_error('验证码错误！'));
            }
            $admin=db('admin')->_as('a')->field("a.*,ar.auth")
                ->join('admin_role ar',"a.role_id=ar.id")
                ->where(['username'=>$name])->find();

            $admin || die(json_error('登录失败1'));
            $salt=$admin['salt'];
//            $md5_pwd=md5(md5($name).$pwd);
            $password=md5(sha1($salt.sha1($salt.sha1($pwd))));
            if($password==$admin['password']){
                session('admin_id',$admin['id']);
                session('admin_info',$admin);
                die(json_success('登录成功',['url'=>U("index/index")]));
            }
            die(json_error('登录失败2'));
        }
       return $this->fetch('index');
    }

    public function verify(){
        $config= [
            // 验证码字符集合
            'codeSet'  => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
            // 验证码字体大小(px)
            'fontSize' => 15,
            // 是否画混淆曲线
            'useCurve' => false,
            // 验证码图片高度
            'imageH'   => 40,
            // 验证码图片宽度
            'imageW'   => 150,
            // 验证码位数
            'length'   => 4,
            // 验证成功后是否重置
            'reset'    => true
        ];
        $g=new Captcha($config);

       return  $g->entry('admin_login');
//        return captcha('login');
    }
}